﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace sdamarketing.Administration
{
    public partial class AddUser : osnova1
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Session["iduser"] == null || Session["username"] == null)
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                    else
                    {
                        if (RolesAndPrivileges.AuthenticateUserWithPanel("Додади корисник", Int32.Parse(Session["idRole"].ToString())) == 0)
                        {
                            Response.Redirect("~/Home.aspx");
                        }
                    }
                    /*
                                    if (RolesAndPrivileges.AuthenticateUserWithPanel(2,Int32.Parse(Session["idRole"].ToString())) == 0)
                                    {
                                        Response.Redirect("~/Home.aspx");
                                    }
                    */
                    DataSet roles = ReturnDataSet.ReturSet("select name, idRole FROM Roles");
                    PrivilegesDropDownList.DataValueField = "idRole";
                    PrivilegesDropDownList.DataTextField = "name";
                    PrivilegesDropDownList.DataSource = roles;
                    PrivilegesDropDownList.DataBind();
                    PrivilegesDropDownList.Items.Insert(0, new ListItem("", ""));
                    PrivilegesDropDownList.ClearSelection();

                    DataSet Sex = ReturnDataSet.ReturSet("SELECT idSex, sex FROM Sex");
                    DropDownListSex.DataValueField = "idSex";
                    DropDownListSex.DataTextField = "sex";
                    DropDownListSex.DataSource = Sex;
                    DropDownListSex.DataBind();
                    DropDownListSex.Items.Insert(0, new ListItem("", ""));

                    DataSet Age = ReturnDataSet.ReturSet("SELECT idAge, fromAndTo FROM Age");
                    DropDownListAge.DataValueField = "idAge";
                    DropDownListAge.DataTextField = "fromAndTo";
                    DropDownListAge.DataSource = Age;
                    DropDownListAge.DataBind();
                    DropDownListAge.Items.Insert(0, new ListItem("", ""));

                    DataSet Education = ReturnDataSet.ReturSet("SELECT idEducation, education FROM Educations");
                    DropDownListEducation.DataValueField = "idEducation";
                    DropDownListEducation.DataTextField = "education";
                    DropDownListEducation.DataSource = Education;
                    DropDownListEducation.DataBind();
                    DropDownListEducation.Items.Insert(0, new ListItem("", ""));

                    DataSet Status = ReturnDataSet.ReturSet("SELECT idSocialStructure, status FROM SocialStructures");
                    DropDownListStructure.DataValueField = "idSocialStructure";
                    DropDownListStructure.DataTextField = "status";
                    DropDownListStructure.DataSource = Status;
                    DropDownListStructure.DataBind();
                    DropDownListStructure.Items.Insert(0, new ListItem("", ""));

                    DataSet Nacionalost = ReturnDataSet.ReturSet("SELECT idNacionalnost,Nacionalnost FROM Nationality");
                    DropDownListNationality.DataValueField = "idNacionalnost";
                    DropDownListNationality.DataTextField = "Nacionalnost";
                    DropDownListNationality.DataSource = Nacionalost;
                    DropDownListNationality.DataBind();
                    DropDownListNationality.Items.Insert(0, new ListItem("", ""));
                }
                catch (Exception exception)
                {
                    SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -addProgram- ");
                }
            }
        }

        protected void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (Users.FindUser(UsernameTextBox.Text.ToString()) == 0)
                {
                    Users.AddUser(FirstNameTextBox.Text, LastNameTextBox.Text, UsernameTextBox.Text, PasswordTextBox.Text, EMailTextBox.Text, QuestionTextBox.Text, AnswerTextBox.Text, 2, Int32.Parse(DropDownListSex.SelectedValue.ToString()), Int32.Parse(DropDownListEducation.SelectedValue.ToString()), Int32.Parse(DropDownListStructure.SelectedValue.ToString()), Int32.Parse(DropDownListAge.SelectedValue.ToString()), Int32.Parse(DropDownListNationality.SelectedValue.ToString()));

    //                if (Users.FindUser(FirstNameTextBox.Text) != 0)
    //                {
                    SaveHistory.addHystoriForUser(Int32.Parse(Session["idUser"].ToString()), Users.FindUser(UsernameTextBox.Text), 1);
    //                }

                    FirstNameTextBox.Text = "";
                    LastNameTextBox.Text = "";
                    UsernameTextBox.Text = "";
                    EMailTextBox.Text = "";
                    QuestionTextBox.Text = "";
                    AnswerTextBox.Text = "";
                    PrivilegesDropDownList.ClearSelection();
                    DropDownListSex.ClearSelection();
                    DropDownListAge.ClearSelection();
                    DropDownListEducation.ClearSelection();
                    DropDownListStructure.ClearSelection();
                    DropDownListNationality.ClearSelection();
                    lblMessage.Text = "Корисникот е успешно додаден";
                }
                else
                {
                    lblMessage.Text = "Корисникот со такво корисничко име веќе постои";
                }
            }
            catch (SqlException exception)
            {
                 lblMessage.Text = "Грешка обидетесе повторно";
                 SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -addUser- ");
            }
        }
    }
}
